from etc import logger
from dbtools.mysql import get_connection
from dbtools.mongo import get_mongodb_linkedin_connection
import requests
import json

log = logger.get_logger('linkedin')
# access_token = 'AQWB7GRxXpwPqf23ActvjDLGlh0BeUDCaglhKqkBafnBti9VmKxl9N8Whs5VHwzmEYaE1XheN_DgWYFQ-k05GnKJeMaxSIrwA_gxRQhXeIXLXvuX12Vec8hUzPRLzY4CcCJ_LoKNX12ohj7rifGni_NO-tOnVUQ_DHSys1bBEQEMKU_YoCw'
access_token = 'AQVXqizqLV_yyaKSln4wdK9CiJ96Z9aeWKdRZH08I6duozSUPKib-82o_gDWUNRXhFqw5WHZNkWompQ41Q4yozG4Na4bjkga5a6tgYnkn_XpcdxyV0N8cA9uq6OKP676mTKQAj3eDRa1HLa6T_SMYoZgJCuO0tYXNDPmdLfe3iyqyqPOWfo'
search_url = 'https://api.linkedin.com/v1/company-search:(companies:(id,name,universal-name,website-url,industries,status,logo-url,blog-rss-url,twitter-id,employee-count-range,specialties,locations,description,stock-exchange,founded-year,end-year,num-followers))?keywords={keywords}&format=json&start={start}&count=20&oauth2_access_token={token}'


def search_name(name):
    start = 0
    while 1:
        url = search_url.format(keywords=name,start = start,token=access_token)
        try:
            r = requests.get(url, timeout=10)
            summary = json.loads(r.text).get('companies')
            _total = summary.get('_total')
            if _total == 0:
                return
            companys = summary.get('values')
            yield companys
            if _total > 20:
                _start = summary.get('_start')
                _count = summary.get('_count')
                if _start+_count >= _total:
                    break
                start += 20
            else:
                break
        except Exception as e:
            # log.exception(e)
            print(r.text)
            raise


def main():
    mysql_connection = get_connection()
    mongodb_success_crunchbase = get_mongodb_linkedin_connection('success_crunchbase_uuid')
    mongodb_account_connection = get_mongodb_linkedin_connection('companys')
    # 已经搜索完成的 crunchbase
    successful_uuid = []
    successful_linked_id = []
    for each in mongodb_success_crunchbase.find({}):
        successful_uuid.append(each['uuid'])
    for each in mongodb_account_connection.find({}):
        successful_linked_id.append(each['id'])

    with mysql_connection.cursor() as cursor:
        sql = "SELECT `uuid`,`name` FROM `organizationsummary` WHERE `facebook_url` IS NOT NULL"
        cursor.execute(sql)
        result = cursor.fetchall()
        for each in result:
            name = each['name']
            if each['uuid'] in successful_uuid:
                continue

            print(name)
            for companys in search_name(name):
                if not companys:
                    break
                for comp in companys:
                    if comp['id'] in successful_linked_id:
                        continue
                    mongodb_account_connection.insert_one(comp)
                    successful_linked_id.append(comp['id'])
            mongodb_success_crunchbase.insert_one({'uuid':each['uuid']})
            # break

if __name__=='__main__':
    main()